package com.pyound.service.impl;

import com.pyound.dao.FastdfsDataDao;
import com.pyound.entity.FastdfsDataEntity;
import com.pyound.service.FastdfsDataService;
import com.pyound.utils.FastDFSUtil;
import org.csource.fastdfs.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;


@Service("fastdfsDataService")
public class FastdfsDataServiceImpl extends ServiceImpl<FastdfsDataDao, FastdfsDataEntity> implements FastdfsDataService {

    @Value("${fastdfs.client-ip}")
    private String fastClientIp;

    @Autowired
    private FastdfsDataDao fastdfsDataDao;

    @Override
    public FastdfsDataEntity upload(String fileName, byte[] buff) {
        FastdfsDataEntity entity=new FastdfsDataEntity();
        String[] upload = FastDFSUtil.upload(StringUtils.getFilenameExtension(fileName), buff);
        entity.setGroupName(upload[0]);
        entity.setFilePath(upload[1]);
        entity.setOriginalFileName(fileName);
        entity.setFileName(upload[1].substring(upload[1].lastIndexOf("/")+1));
        entity.setFileExt(StringUtils.getFilenameExtension(fileName));
        entity.setCreateTime(new Date());
        //查询当前文件信息
        FileInfo fileInfo = FastDFSUtil.getFileInfo(upload[0], upload[1]);
        entity.setFileSize(fileInfo.getFileSize());
        entity.setAccessAddress(fastClientIp+"/"+upload[0]+"/"+upload[1]);

        //将文件信息保存到数据库
        fastdfsDataDao.insert(entity);

        return entity;
    }
}